<?php
require_once 'lib/controllerWithoutGetOrPost.php';
require_once 'view/standard-top.php';

//CHECK for logged in!

function sendMail($to,$subject,$text,$sender){
    $headers = "From: " . $sender . "\r\n";
    if(mail($to,$subject,$text,$headers)){return 1;}else{return 0;}
}

if($_POST['send']=='mail'){
    $UserID=$_SESSION['user'];
    $sql = 'SELECT `Username` , `Email` FROM `Users` WHERE `ID` = '.$UserID;
    $result = $db->query($sql);
    if ($db->rowCount($result) > 0) {
        $row = $db->fetch($result);
        $userEmail = $row['Email'];
        $userName = $row['Username'];
    }else{
        $userEmail = 'none@amemorytree.co.nz';
        $userName = 'Anon';
    }
    $to = 'funeralmatters@amemorytree.co.nz';
    $subject = $_POST['subject'].' - '.$userName;
    $text = $_POST['content'];
    
    if(sendMail($to,$subject,$text,$userEmail)==1){
        echo <<< EOT
            <div id='centerbar'>
                <h3 style='color: #c12a0b;'>Message Successfully Sent</h3><br />
                Thanks for contacting us, we value your support.<br /><br />
                Please allow 24 hours for us to action your request.<br /><br />
                The Team @ <a href="http://www.amemorytree.co.nz">www.amemorytree.co.nz</a>
            </div>
EOT;
    }else{echo 'Failed to send!<br />Please try again.';}
}else{
    switch($_GET['topic']){
    case "submitnotice":
      $SelectedTopic='Notice';
      break;
    case "submitfeedback":
      $SelectedTopic='Feedback';
      break;
    case "other":
      $SelectedTopic='Other';
      break;
    case "contactadmin":
      $SelectedTopic='Admin';
      break;
    default:
      $SelectedTopic='Admin';
    }
    echo <<< EOT
        <div id='centerbar'>
            <h3>Contact Us Today</h3>
            Please select an option below and complete the form.<br /><br />
            We will action your request within 24 hours.<br /><br />
            <form name='membercontact' action='membercontact.php' method='post'>
                <input type='hidden' name='send' value='mail' />
EOT;
    echo "<label><input type='radio' name='subject' value='Submit Death Notice' ";
    if($SelectedTopic=='Notice'){echo "checked='checked' ";}
    echo "/> Submit a Death Notice</label><br />";
    echo "<label><input type='radio' name='subject' value='Feedback' ";
    if($SelectedTopic=='Feedback'){echo "checked='checked' ";}
    echo "/> Submit Feedback</label><br />";
    echo "<label><input type='radio' name='subject' value='Contact Admin' ";
    if($SelectedTopic=='Admin'){echo "checked='checked' ";}
    echo "/> Contact Administrator</label><br />";
    echo "<label><input type='radio' name='subject' value='Other' ";
    if($SelectedTopic=='Other'){echo "checked='checked' ";}
    echo "/> Other</label><br /><br />";
    echo <<< EOT
                <div style='position:absolute;'>
                    <input type='submit' class='button' style='position:absolute;top:-30px;right:0px;' value='Send'><br />
                    <textarea cols='75' rows='25' name='content' style='position:relative;top:-20px;'></textarea><br />
                    <input type='submit' class='button' style='position:absolute;bottom:-5px;right:0px;' value='Send'>
                </div>
            </form>
        </div>
EOT;
}

require_once 'view/standard-noads-bottom.php';
?>